/*******************************************************************************
Paper: Does the Production Approach to Markup Estimation Match a Stylized Fact?
Description: Clean financial accounts data
Author: Tove Forsbacka
Last Updated: 15 February 2026
*******************************************************************************/

* Setup and environment control
clear
set more off
capture log close 
version 18.5
*ssc install hhi

* Define path and load the data
*cd "PROJECT_PATH"
use "bokslut.dta"

* Keep relevant variables
keep ORGNR REDTYP ANTANST BSLSLUT BSLSTART NTOMS RAVAR HANDVAR KOSALVAR PERSKOS ///
 LONOV SOCKOSTN AVSKSALV MATANLSU

* Drop if the number of employees is missing or zero
drop if ANTANST == 0
drop if ANTANST == .

* Measure costs in positive amounts and drop if data is missing

drop if NTOMS == .
drop if NTOMS <=0

replace PERSKOS = -PERSKOS
egen PERSKOS_new = rowtotal(LONOV SOCKOSTN)
replace PERSKOS = PERSKOS_new if PERSKOS==.
drop PERSKOS_new
drop if PERSKOS ==.
drop if PERSKOS <=0
drop LONOV SOCKOSTN

replace RAVAR = -RAVAR
drop if RAVAR <0 & REDTYP =="K" | REDTYP =="BK" | REDTYP =="FK" | REDTYP =="XK" ///
 | REDTYP=="RO"

replace HANDVAR = -HANDVAR
drop if HANDVAR <0 & REDTYP =="K" | REDTYP =="BK" | REDTYP =="FK" | REDTYP =="XK" ///
 | REDTYP=="RO"

replace KOSALVAR = -KOSALVAR
drop if KOSALVAR <0 & REDTYP =="F" | REDTYP =="BF" | REDTYP =="FF" | REDTYP =="XF" 

replace AVSKSALV = -AVSKSALV
drop if AVSKSALV <0 & REDTYP =="F" | REDTYP =="BF" | REDTYP =="FF" | REDTYP =="XF" 


* Change from fiscal year to calendar year

gen year = year(BSLSTART)

drop if year < 1997

* Keep observation where the reporting year is a full year and there is one unique report per firm
gen duration_months=(year(BSLSLUT)-year(BSLSTART))*12+month(BSLSLUT)-month(BSLSTART)
keep if duration_months == 11
drop duration_months 

bysort ORGNR year: gen dup = _N
drop if dup > 1
drop dup

* Set panel
xtset ORGNR year 

bysort ORGNR (year): gen N = _n
bysort ORGNR: egen max = max(N)
gen finalyear = 1 if N==max
replace finalyear = 0 if finalyear==.

gen end_month = month(BSLSLUT)

* Adjusting variables for fiscal year
foreach var of varlist NTOMS RAVAR HANDVAR KOSALVAR PERSKOS MATANLSU AVSKSALV {
	gen `var'_thisyear = `var' * end_month/12
	gen `var'_lastyear = `var' * (12-end_month)/12
	bysort ORGNR: gen `var'_f = `var'_thisyear + `var'_lastyear[_n+1]
	replace `var'_f=`var'_thisyear if finalyear==1
	drop `var' `var'_thisyear `var'_lastyear
	rename `var'_f `var'
	}

drop BSLSTART BSLSLUT end_month finalyear max N 

* Drop 1997 since Serrano database starts in 1998
drop if year ==1997 

* Add additional variables from the Serrano database
merge 1:1 ORGNR year using "serrano_variables.dta"
drop if _merge==2
drop _merge

* Sample: keep firms that are private, active, non-financial

* Keep only firms in private sector
keep if ser_ftgkategori ==30
drop ser_ftgkategori

* Keep only firms active during the year
keep if ser_aktiv==1
drop ser_aktiv

* Rename industry codes
rename bransch_sni071 SNI2007_1
rename bransch_sni072 SNI2007_2
rename bransch_sni073 SNI2007_3
rename bransch_sni074 SNI2007_4
rename bransch_sni075 SNI2007_5

rename bransch_sni1 SNI2002_1
rename bransch_sni2 SNI2002_2
rename bransch_sni3 SNI2002_3
rename bransch_sni4 SNI2002_4
rename bransch_sni5 SNI2002_5

* Drop if primary industry code is missing
drop if SNI2007_1==. & SNI2002_1==.

* Sectors at 2-digit level (SNI/NACE)
foreach var of varlist SNI200* {
	tostring `var', gen(str_`var')
	replace str_`var' = "0" + str_`var' if length(str_`var')==4
	gen group_`var' = substr(str_`var',-5,2)
	drop str_`var'
	destring group_`var', generate(n_group_`var')
	drop group_`var'
	rename n_group_`var' group_`var'
}

* Drop finanical firms 

foreach var of varlist SNI2002* {
drop if 	`var' == 65110 | `var' == 65120 | `var' == 65210 | `var' == 65220 | ///
			`var' == 65231 | `var' == 65232 | `var' == 66011 | `var' == 66012 | ///
			`var' == 66020 | `var' == 66030 | `var' == 67110 | `var' == 67120 | ///
			`var' == 67130 | `var' == 67201 | `var' == 67202 | `var' == 74150
}

foreach var of varlist SNI2007* {
drop if 	`var' == 64201 | `var' == 64202 | `var' == 64301 | `var' == 64309 | /// 
			`var' == 66301 | `var' == 66309 | `var' == 70100 | `var' == 70220
}

foreach var of varlist group_SNI2002_* {
drop if `var' == 65 | `var' == 66 | `var' == 67
}

foreach var of varlist group_SNI2007_* {
drop if `var' == 64 | `var' == 65 | `var' == 66
}
drop group*

* Convert industry codes SNI2002 into SNI2007
merge m:1 SNI2002_1 using "sni_key"
drop if _merge == 2
drop _merge

replace SNI2007_1 = SNI2007_1_new if SNI2007_1 == .
drop if SNI2007_1==.
drop if SNI2007_1==0

drop *_new SNI2002* SNI2007_2-SNI2007_5

* Adjust variables using GDP deflator
merge m:1 year using "gdp_deflator.dta"
drop if _merge == 2
drop _merge

gen dNTOMS = NTOMS/gdpdeflator*100 
gen dRAVAR = RAVAR/gdpdeflator*100
gen dHANDVAR = HANDVAR/gdpdeflator*100
gen dKOSALVAR = KOSALVAR/gdpdeflator*100
gen dPERSKOS = PERSKOS/gdpdeflator*100
gen dMATANLSU = MATANLSU/gdpdeflator*100
gen dAVSKSALV = AVSKSALV/gdpdeflator*100

drop NTOMS RAVAR HANDVAR KOSALVAR PERSKOS MATANLSU AVSKSALV gdpdeflator 

* Creating variable for intermediate input

gen dINTINPUT = .

egen dINTINPUT_k = rowtotal(dRAVAR dHANDVAR) if REDTYP =="K" | REDTYP =="BK" ///
 | REDTYP =="FK" | REDTYP =="XK" | REDTYP=="RO", missing

egen OTHER= rowtotal(dPERSKOS dAVSKSALV) if REDTYP =="F" | REDTYP =="BF" ///
 | REDTYP =="FF" | REDTYP =="XF", missing

egen dCOGS = rowtotal(dPERSKOS dRAVAR dHANDVAR) if REDTYP =="K" | REDTYP =="BK" ///
 | REDTYP =="FK" | REDTYP =="XK" | REDTYP=="RO", missing
replace dCOGS = dKOSALVAR if REDTYP =="F" | REDTYP =="BF" | REDTYP =="FF" ///
 | REDTYP =="XF"
drop if dCOGS == .
drop if dCOGS == 0

gen dINTINPUT_f = dCOGS-OTHER if REDTYP =="F" | REDTYP =="BF" | REDTYP =="FF" ///
 | REDTYP =="XF"

replace dINTINPUT = dINTINPUT_k if REDTYP =="K" | REDTYP =="BK" | REDTYP =="FK" ///
 | REDTYP =="XK" | REDTYP=="RO"
replace dINTINPUT = dINTINPUT_f if REDTYP =="F" | REDTYP =="BF" | REDTYP =="FF" ///
 | REDTYP =="XF"

drop dINTINPUT_k dINTINPUT_f dRAVAR dHANDVAR dAVSKSALV OTHER dCOGS REDTYP dKOSALVAR 
drop if dINTINPUT == .
drop if dINTINPUT < 0

format dNTOMS %12.0f
format dPERSKOS %12.0f
format dINTINPUT %12.0f
format dMATANLSU %12.0f

* Add additional data on markets
rename ser_kommunx Kommunkod

drop if Kommunkod==.

* Municipality and region
merge m:1 Kommunkod using "municipalities.dta"
drop if _merge==2
drop _merge

* Population density
merge m:1 Kommunkod year using "population_density.dta"
drop if _merge==2
drop _merge

gen lpopulation_density = log(population_density)

* Income
merge m:1 Kommunkod year using "income.dta"
drop if _merge==2
drop _merge

gen lincome = log(income)

* Create indicator for the largest regions (Stockholm and Gothenburg)
gen stockholm = 0
replace stockholm = 1 if Kommunnamn == "Stockholm" | Kommunnamn == "Botkyrka" ///
 | Kommunnamn == "Danderyd" | Kommunnamn == "Ekerö" | Kommunnamn == "Haninge" ///
 | Kommunnamn == "Huddinge" | Kommunnamn == "Järfälla" | Kommunnamn == "Lidingö" ///
 | Kommunnamn == "Nacka" | Kommunnamn == "Norrtälje" | Kommunnamn == "Nykvarn" ///
 | Kommunnamn == "Nynäshamn" | Kommunnamn == "Salem" | Kommunnamn == "Sigtuna" ///
 | Kommunnamn == "Sollentuna" | Kommunnamn == "Solna" | Kommunnamn == "Sundbyberg" ///
 | Kommunnamn == "Södertälje" | Kommunnamn == "Tyresö" | Kommunnamn == "Täby" ///
 | Kommunnamn == "Upplands-Bro" | Kommunnamn == "Upplands Väsby" ///
 | Kommunnamn == "Vallentuna" | Kommunnamn == "Vaxholm" | Kommunnamn == "Värmdö" ///
 | Kommunnamn == "Österåker" 

gen goteborg= 0
replace goteborg= 1 if Kommunnamn == "Göteborg" | Kommunnamn == "Ale" ///
 | Kommunnamn == "Alingsås" | Kommunnamn == "Härryda" | Kommunnamn == "Kungsbacka" ///
 | Kommunnamn == "Kungsälv" | Kommunnamn == "Lerum" | Kommunnamn == "Lilla Edet" ///
 | Kommunnamn == "Mölndal" | Kommunnamn == "Partille" | Kommunnamn == "Stenungsund" ///
 | Kommunnamn == "Tjörn" | Kommunnamn == "Öckerö"

* Gen indicators for the four sectors I focus on
gen market_hairdresser = 0
replace market_hairdresser = 1 if SNI2007_1 == 96021

gen market_driving = 0
replace market_driving = 1 if SNI2007_1 == 85530 

gen market_taxi = 0
replace market_taxi = 1 if SNI2007_1 == 49320

gen market_grocery = 0
replace market_grocery = 1 if SNI2007_1 == 47112

* Drop all other sectors 
keep if market_hairdresser==1 | market_driving==1 | market_taxi==1 | market_grocery==1

* Manual cleaning of markets:
* 1) Idenfity firms belonging to chains and the markets where they were active
* 2) Drop firms that have the same industry code but do not belong to the markets I study

* Names in capital letters
replace ser_namn = upper(ser_namn)

* Gen indicator for chains
gen chain=0

* Market 1: Hairdressers

* Gen indicator for chains in the haridresser sector
gen market_hairdresser_chain = 0 

* Chains present only in Stockholm and Gothenburg 
replace chain = 1 if ORGNR == 5560990052 
replace chain = 1 if ORGNR == 5565509188 
replace chain = 1 if ORGNR == 5566521745 
replace chain = 1 if ORGNR == 5565143574 
replace chain = 1 if ORGNR == 5569763518 
replace chain = 1 if ORGNR == 5569763047 

* Chains present in other markets
replace chain = 1 if ORGNR == 5566274790 
replace market_hairdresser_chain = 1 if Kommunnamn=="Malmö" & year >= 2003
replace market_hairdresser_chain = 1 if Kommunnamn=="Örnsköldsvik"  & year >= 2003
replace market_hairdresser_chain = 1 if Kommunnamn=="Nynäshamn"  & year >= 2003

replace chain = 1 if ORGNR == 5566956404 
replace market_hairdresser_chain = 1 if Kommunnamn=="Borlänge" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Falun" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Gävle" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Östersund" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Luleå" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Örebro" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Norrköping" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Malmö" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Helsingborg" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Kristianstad" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Kävlinge" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Karlstad" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Umeå" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Västerås" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Sala" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Skövde" & year >= 2011
replace market_hairdresser_chain = 1 if Kommunnamn=="Nyköping" & year >= 2011

replace chain = 1 if ORGNR == 5564702354 
replace chain = 1 if ORGNR == 5590412689 
replace market_hairdresser_chain = 1 if Kommunnamn=="Härnösand" & year >= 2002 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Luleå" & year >= 2002 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Skellefteå" & year >= 2002 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Umeå" & year >= 2002 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Örnsköldsvik" & year >= 2002 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Östersund" & year >= 2002 & year <= 2018

replace chain = 1 if ORGNR == 5564234226 
replace market_hairdresser_chain = 1 if Kommunnamn=="Jönköping" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Mörbylånga" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Kalmar" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Borgholm" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Mönsterås" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Hultsfred" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Vimmerby" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Linköping" & year >= 1999 
replace market_hairdresser_chain = 1 if Kommunnamn=="Nybro" & year >= 1999 

replace chain = 1 if ORGNR == 5564418761
replace market_hairdresser_chain = 1 if Kommunnamn=="Kävlinge" & year >= 2007 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Sollefteå" & year >= 2007 & year <= 2018
replace market_hairdresser_chain = 1 if Kommunnamn=="Östersund" & year >= 2007 & year <= 2018

replace chain = 1 if ORGNR == 5565509220 
replace market_hairdresser_chain = 1 if Kommunnamn=="Lund" & year >= 2000 
replace market_hairdresser_chain = 1 if Kommunnamn=="Kristianstad" & year >= 2000 
replace market_hairdresser_chain = 1 if Kommunnamn=="Malmö" & year >= 2000 
replace market_hairdresser_chain = 1 if Kommunnamn=="Växjö" & year >= 2000 

replace chain = 1 if ORGNR == 5566804240
replace market_hairdresser_chain = 1 if Kommunnamn=="Eskilstuna" & year >= 2007
replace market_hairdresser_chain = 1 if Kommunnamn=="Västerås" & year >= 2007

* Drop firms that have the same industry code but that are not hairdressers
drop if ORGNR == 5565753018 
drop if ORGNR == 5566162607	
drop if ORGNR == 5563493633	
drop if ORGNR == 5567555007 
drop if ORGNR == 5562774702 

* Market 2: driving schools

* Based on firm name, drop firms that have the same industry code but that are not driving schools
drop if strpos(ser_namn, "HELICOPTER")
drop if strpos(ser_namn, "HELIKOPTER")
drop if strpos(ser_namn, "AVIATION")
drop if strpos(ser_namn, "FLIGHT")
drop if strpos(ser_namn, "FLYG")
drop if strpos(ser_namn, "YACHT")
drop if strpos(ser_namn, "HALKBANA")
drop if strpos(ser_namn, "VNINGSBANA")
drop if strpos(ser_namn, "SEGLAR")
drop if strpos(ser_namn, "SEGLING")
drop if strpos(ser_namn, "TRUCK")
drop if strpos(ser_namn, "LASTBIL")

* Gen indicator for chains in the driving school sector
gen market_driving_chain = 0 

* Chains present only in Stockholm an Gothenburg 
replace chain = 1 if ORGNR == 5560456252 
replace chain = 1 if ORGNR == 5560456252 

* Chains present in other markets
replace chain = 1 if ORGNR == 5561743765 
replace market_driving_chain = 1 if Kommunnamn=="Västerås" & year >= 1999
replace market_driving_chain = 1 if Kommunnamn=="Köping" & year >= 1999
replace market_driving_chain = 1 if Kommunnamn=="Eskilstuna" & year >= 1999

replace chain = 1 if ORGNR == 5565927091 
replace market_driving_chain = 1 if Kommunnamn=="Luleå" & year >= 2001
replace market_driving_chain = 1 if Kommunnamn=="Piteå" & year >= 2001
replace market_driving_chain = 1 if Kommunnamn=="Älvsbyn" & year >= 2001

replace chain = 1 if ORGNR == 5563532513 
replace market_driving_chain = 1 if Kommunnamn=="Malmö" & year >= 1998
replace market_driving_chain = 1 if Kommunnamn=="Trelleborg" & year >= 1998

replace chain = 1 if ORGNR == 5567115174 
replace market_driving_chain = 1 if Kommunnamn=="Kungälv" & year >= 2007
replace market_driving_chain = 1 if Kommunnamn=="Stenungsund" & year >= 2007

replace chain = 1 if ORGNR == 5562534460	
replace market_driving_chain = 1 if Kommunnamn=="Strömsund" & year >= 1998
replace market_driving_chain = 1 if Kommunnamn=="Östersund" & year >= 1998

replace chain = 1 if ORGNR == 9165486623	
replace market_driving_chain = 1 if Kommunnamn=="Trelleborg" & year == 2010 ///
 | year == 2011 | year == 2013
replace market_driving_chain = 1 if Kommunnamn=="Mölndal" & year >= 2010 ///
 | year == 2011 | year == 2013
replace market_driving_chain = 1 if Kommunnamn=="Ystad" & year >= 2010 ///
 | year == 2011 | year == 2013
replace market_driving_chain = 1 if Kommunnamn=="Landskrona" & year >= 2010 ///
 | year == 2011 | year == 2013

replace chain = 1 if ORGNR == 5566126719	
replace market_driving_chain = 1 if Kommunnamn=="Sundsvall" & year >= 2002
replace market_driving_chain = 1 if Kommunnamn=="Timrå" & year >= 2002

* Drop firms that have the same industry code but that are not driving schools
drop if ORGNR == 5561820910 
drop if ORGNR == 5561034025	
drop if ORGNR == 5565999835	
drop if ORGNR == 5563846087	
drop if ORGNR == 5567592869	
drop if ORGNR == 5566090881	
drop if ORGNR == 5565649869	
drop if ORGNR == 5566266788	
drop if ORGNR == 5562248814	
drop if ORGNR == 5562127737	
drop if ORGNR == 5565211611	
drop if ORGNR == 5565237400	
drop if ORGNR == 5562933472	
drop if ORGNR == 5564427556	
drop if ORGNR == 5563172971	
drop if ORGNR == 5569303109	
drop if ORGNR == 5568035488	
drop if ORGNR == 5560485863	
drop if ORGNR == 5567336879	
drop if ORGNR == 5566621123	
drop if ORGNR == 5563187821	
drop if ORGNR == 5564896636 
drop if ORGNR == 5565017695 
drop if ORGNR == 5565825931	
drop if ORGNR == 5563280329	
drop if ORGNR == 5563780906	
drop if ORGNR == 5561887992 
drop if ORGNR == 5566404694	
drop if ORGNR == 5567440606	
drop if ORGNR == 5568229107	
drop if ORGNR == 5562242031	
drop if ORGNR == 5565701843	
drop if ORGNR == 5562176205	
drop if ORGNR == 5562426188	
drop if ORGNR == 5569773368	
drop if ORGNR == 5565277075		
drop if ORGNR == 5563053494	
drop if ORGNR == 5562426188	
drop if ORGNR == 5563276475 
drop if ORGNR == 5565284790 
drop if ORGNR == 5565330346 
drop if ORGNR == 5566139092 	
drop if ORGNR == 5562629179 
drop if ORGNR == 5565515508	
drop if ORGNR == 5566961735	
drop if ORGNR == 5566875026 
drop if ORGNR == 5564146412	
drop if ORGNR == 5563332393	
drop if ORGNR == 5567156368	
drop if ORGNR == 5567205884 
drop if ORGNR == 5565970869	
drop if ORGNR == 5565042974	
drop if ORGNR == 5567472781	
drop if ORGNR == 5568212699	
drop if ORGNR == 5562647346	
drop if ORGNR == 5567295273	
drop if ORGNR == 5569356370 
drop if ORGNR == 5564690351	
drop if ORGNR == 5566098579	
drop if ORGNR == 5561361907 
drop if ORGNR == 5566565254	
drop if ORGNR == 5566757257	
drop if ORGNR == 5564164266	
drop if ORGNR == 5566073408	

* Market 3: Grocery stores

* Gen indicator for chains in the grocery sector
* (Chains in the grocery sector are primarily handled through an alternative robustness test)
gen market_grocery_chain = 0 
replace chain = 1 if ORGNR == 5560305921 
replace chain = 1 if ORGNR == 5569813156	
replace chain = 1 if ORGNR == 5569813172	
replace chain = 1 if ORGNR == 5562055227	
replace chain = 1 if ORGNR == 5561632232	
replace chain = 1 if ORGNR == 5563593671	
replace chain = 1 if ORGNR == 5560901968	
replace chain = 1 if ORGNR == 5563146751	
replace chain = 1 if ORGNR == 9696676312	
replace chain = 1 if ORGNR == 5561138826	
replace chain = 1 if ORGNR == 5566152269	
replace chain = 1 if ORGNR == 5562373547	
replace chain = 1 if ORGNR == 9168354463	
replace chain = 1 if ORGNR == 5560864315	
replace chain = 1 if ORGNR == 5563593671	
replace chain = 1 if ORGNR == 5565036141 	
replace chain = 1 if ORGNR == 5562868488	
replace chain = 1 if ORGNR == 5561776575 	
replace chain = 1 if ORGNR == 5565972451	
replace chain = 1 if ORGNR == 5561887034		

* Market 4: Taxi

* Based on firm name, drop firms that have the same industry code but that are not taxis
* Drop firms with "färdtjänst" in the name 
* (Swedish letter ä incorrectly coded, I therefore focus on the four middle letters)
drop if strpos(ser_namn, "RDTJ")

* Gen indicator for chains in the taxi sector
gen market_taxi_chain = 0

* Chains present only in Stockholm and Gothenburg
replace chain = 1 if ORGNR == 5560279811
replace chain = 1 if ORGNR == 5565286399	
replace chain = 1 if ORGNR == 5562081215

* Chains present in other markets
replace chain = 1 if ORGNR == 5565525424	
replace market_taxi_chain = 1 if Kommunnamn=="Bjuvs" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Bromölla " & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Burlöv" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Båstad" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Eslöv" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Helsingborg" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Hässleholm" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Höganäs" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Hörby" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Höör" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Klippan" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Kristianstad" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Kävlinge" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Landskrona" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Lomma" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Lund" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Malmö" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Osby" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Perstorp" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Simrishamn" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Sjöbo" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Skurup" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Staffanstorp" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Svalöv" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Svedala" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Tomelilla" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Trelleborg" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Vellinge" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Ystad" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Åstorp" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Ängelholm" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Örkelljunga" & year >= 2000 & year <=2009
replace market_taxi_chain = 1 if Kommunnamn=="Östra Göinge" & year >= 2000 & year <=2009

replace chain = 1 if ORGNR == 5568467145	
replace market_taxi_chain = 1 if Kommunnamn=="Eskilstuna" & year >= 2012
replace market_taxi_chain = 1 if Kommunnamn=="Örebro" & year >= 2012

replace chain = 1 if ORGNR == 5564670288	
replace market_taxi_chain = 1 if Kommunnamn=="Eskilstuna" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Linköping" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Norrköping" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Valdemarsvik" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Västervik" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Östergötland" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Söderköping" & year >= 1998

replace chain = 1 if ORGNR == 5566374483	
replace market_taxi_chain = 1 if Kommunnamn=="Kristinehamn" & year >= 2003 & year <=2013
replace market_taxi_chain = 1 if Kommunnamn=="Hagfors" & year >= 2003 & year <=2013
replace market_taxi_chain = 1 if Kommunnamn=="Torsby" & year >= 2003 & year <=2013

replace chain = 1 if ORGNR == 5562259589	
replace chain = 1 if ORGNR == 5563096147	
replace market_taxi_chain = 1 if Kommunnamn=="Boxholm" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Finspång" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Kinda" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Linköping" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Mjölby" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Motala" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Norrköping" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Söderköping" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Vadstena" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Valdemarsvik" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Ydre" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Åtvidaberg" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Ödeshög" & year >= 1998

replace chain = 1 if ORGNR == 5568657869	
replace market_taxi_chain = 1 if Kommunnamn=="Katrineholm"  & year >= 2013 & year <=2015
replace market_taxi_chain = 1 if Kommunnamn=="Eskilstuna" & year >= 2013 & year <=2015

replace chain = 1 if ORGNR == 5563566479	
replace market_taxi_chain = 1 if Kommunnamn=="Avesta" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Borlänge" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Falun" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Gagnef" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Hedemora" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Leksand" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Ludvika" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Malung-Sälen" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Mora" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Orsa" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Rättvik" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Smedjebacken" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Säter" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Vansbro" & year >= 1998
replace market_taxi_chain = 1 if Kommunnamn=="Älvdalen" & year >= 1998

* Based on firm name, drop firms that have the same industry code but that are not taxis
drop if ORGNR == 5568018245		
drop if ORGNR == 5562690320		
drop if ORGNR == 5569293987		
drop if ORGNR == 5565222113		
drop if ORGNR == 5563573459 	
drop if ORGNR == 5567214530		
drop if ORGNR == 5563602522		
drop if ORGNR == 5565844346		
drop if ORGNR == 5561429217		
drop if ORGNR == 5567592596		
drop if ORGNR == 5565579785		
drop if ORGNR == 5565529871		
drop if ORGNR == 5564363645		
drop if ORGNR == 5564042579		
drop if ORGNR == 5562112630		
drop if ORGNR == 5561575654		
drop if ORGNR == 5565844346		
drop if ORGNR == 5563700425			
drop if ORGNR == 5567526966		
drop if ORGNR == 5561554709		
drop if ORGNR == 5564894177		
drop if ORGNR == 5562724574		
drop if ORGNR == 5566357645	
drop if ORGNR == 5562934603		
drop if ORGNR == 5564827607	
drop if ORGNR == 5562868132			
drop if ORGNR == 5562370402		
drop if ORGNR == 5567078638	
drop if ORGNR == 5566673728		
drop if ORGNR == 5564962966		
drop if ORGNR == 5563952638	
drop if ORGNR == 5563952638	
drop if ORGNR == 5564931177	
drop if ORGNR == 5567503213	
drop if ORGNR == 5566288568	
drop if ORGNR == 5563829703	
drop if ORGNR == 5562903913	
drop if ORGNR == 5568547169	
drop if ORGNR == 5564894177	
drop if ORGNR == 5563293074	
drop if ORGNR == 5568049109	
drop if ORGNR == 5564007481		
drop if ORGNR == 5565039657		
drop if ORGNR == 5562911965		
drop if ORGNR == 5561484626		
drop if ORGNR == 5562903913		
drop if ORGNR == 5562911965		
drop if ORGNR == 5562690833		
drop if ORGNR == 5569389298		
drop if ORGNR == 5568987373		
drop if ORGNR == 5566836762		
drop if ORGNR == 5567150759		
drop if ORGNR == 5568217144		
drop if ORGNR == 5568702947		
drop if ORGNR == 5564653235		
drop if ORGNR == 5562121326		
drop if ORGNR == 5566860812		
drop if ORGNR == 5566391578		
drop if ORGNR == 5565718912		
drop if ORGNR == 5561262428		
drop if ORGNR == 5565779443		
drop if ORGNR == 5567479059		
drop if ORGNR == 5564730405		
drop if ORGNR == 5566385349		
drop if ORGNR == 5563912681		
drop if ORGNR == 5564846979		
drop if ORGNR == 5565136891		
drop if ORGNR == 5565957254		
drop if ORGNR == 5564439338		

drop ser_namn 

* Summary statistics
sum ANTANST if market_grocery==1
sum ANTANST if market_taxi==1
sum ANTANST if market_driving==1
sum ANTANST if market_hairdresser==1

* Drop firms with only one employee (five or less for grocery sector)
drop if ANTANST <= 5 & market_grocery == 1
drop if ANTANST < 2

* Create measures of market concentration

save "temp_data1.dta", replace

foreach var of varlist market_hairdresser market_driving market_grocery market_taxi {
	use "temp_data1.dta", clear 
	keep if `var' == 1

* Indicator for presence of a chain on a market
bysort Kommunkod year: egen market_chain = max(`var'_chain)
drop if chain == 1

* Number of firms
sort Kommunkod year
bysort Kommunkod year: gen count = _n
bysort Kommunkod year: egen firms = max(count)

* Monopoly markets
gen monopoly = 0
replace monopoly = 1 if firms == 1

* Duopoly markets
gen duopoly = 0
replace duopoly = 1 if firms == 2

* Triopoly markets
gen triopoly = 0
replace triopoly = 1 if firms == 3

* Herfindahl-Hirschman Index
hhi dNTOMS, by(Kommunkod year)
label variable hhi_dNTOMS "HHI

save "`var'.dta", replace
 }

use "market_hairdresser.dta", clear

foreach var of varlist market_driving market_grocery market_taxi {
append using "`var'.dta"
}

drop market_*_chain chain

* Generate indicator for each local market
egen localmarket = group(SNI2007_1 Kommunkod)

* Saving data
save "bokslut_clean.dta", replace
